package com.leetcode.algorithm.回溯;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class lc46全排列 {
    private static int n;
    private static String[] parts;
    private static String line;

    private static List<List<Integer>> res=new ArrayList<>();
    private static List<Integer> path=new ArrayList<>();

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while((line=in.readLine())!=null){
            parts=line.split(" ");
            n= parts.length;
            int[] nums=new int[n];
            for(int i=0;i<n;i++){
                nums[i]=(Integer.parseInt(parts[i]));
            }
            res.clear();
            path.clear();
            permute(nums);
            for(List<Integer> list:res){
                for(int i:list){
                    System.out.print((i + " "));
                }
                System.out.println();
            }
        }
        out.flush();
        out.close();
    }

    private static void permute(int[] nums) {
        dfs(nums,0);
    }

    private static void dfs(int[] nums,int index) {
        if(index== nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (path.contains(nums[i])){
                continue;
            }
            path.add(nums[i]);
            dfs(nums,index+1);
            path.remove(path.size()-1);
        }
    }
}
